<?php
class ActivityController extends Controller
{
    const TYPE_WHEEL_ACTION = 'wheel';
    const TYPE_SCRATCHCARD_ACTION = 'scratchcard';
    const TYPE_SMASHEGGS_ACTION = 'smasheggs';

    const TYPE_WHEEL_DES = '大转盘';
    const TYPE_SCRATCHCARD_DES = '刮刮卡';
    const TYPE_SMASHEGGS_DES = '砸金蛋';

    const PRIZE_DES = '实物';

    public $layout = '//layouts/basic_actives';

    public function actionWheel(){
        $this->render('wheel');
    }

    public function actionScratchcard(){

        $this->render('scratchcard');
    }

    public function actionSmasheggs(){
        $this->render('smasheggs');
    }

    public function actionActivityDetail(){
        $id_wxuser_mcard = app()->request->getParam('id_wxuser_mcard', null);
        $id_onlineactivity = app()->request->getParam('id_onlineactivity', null);
        $onlineactivity_action = app()->request->getParam('onlineactivity_action',null);
        $wxuserMcard = WxuserMcard::model()->findByPk($id_wxuser_mcard);
        $activity = Onlineactivity::model()->findByPk($id_onlineactivity);
        if($wxuserMcard && $activity){
            switch ($onlineactivity_action) {
                case self::TYPE_WHEEL_DES:
                    $action = self::TYPE_WHEEL_ACTION;
                    break;

                case self::TYPE_SCRATCHCARD_DES:
                    $action = self::TYPE_SCRATCHCARD_ACTION;
                    break;

                case self::TYPE_SMASHEGGS_DES:
                    $action = self::TYPE_SMASHEGGS_ACTION;
                    break;

                default:
                    break;
            }
            $this->render($action,array(
                'wxuserMcard' => $wxuserMcard,
                'activity' => $activity,
                'onlineactivity_action'=>$onlineactivity_action
            ));
        }
    }

    public function actionPrizeRecord(){
        $onlineactivity_action = app()->request->getParam('onlineactivity_action',null);
        $id_wxuser_mcard = app()->request->getParam('id_wxuser_mcard', null);
        $id_onlineactivity = app()->request->getParam('id_onlineactivity', null);
        $wxuserMcard = WxuserMcard::model()->findByPk($id_wxuser_mcard);
        if($wxuserMcard){
            $this->render('prizerecord',array(
                'wxuserMcard' => $wxuserMcard,
                'id_onlineactivity' => $id_onlineactivity,
                'onlineactivity_action'=>$onlineactivity_action
            ));
        }
    }

    protected function _MD5Sign($data, $token)
    {
        //拿到传过来值不为空的数据
        $data = \array_filter($data);
        ksort($data);
        $str = implode('', $data);
        return md5($str . '&' . $token);
    }

    public function actionwxtestpay()
    {

        $data = array(
            'merchant_id' => '80000001',
            'submch_accnt_no' => 'T0020180906224006000010',
            'total_amount' => '0.01',
            'notify_url' => 'http://testallpay.visastandards.com/Refundnotiy/ReciveNotiy',
            'trade_no' => 'lztest'.time(),
            'callback_url' => 'http://ww.baidu.com',
            'sign_type' => 'md5',
        );
        $data['sign'] = $this->_MD5Sign($data, '9b389216c3c5vtlwf535510b33b9e6ea7');

        $this->render('wxtestpay', array(
            'data' => $data
        ));
    }

    public function actionalitestpay()
    {

        $data = array(
            'merchant_id' => '80000001',
            'submch_accnt_no' => 'T0020180906224006000010',
            'total_amount' => '0.01',
            'notify_url' => 'http://testallpay.visastandards.com/Refundnotiy/ReciveNotiy',
            'trade_no' => 'lztest'.time(),
            'callback_url' => 'http://ww.baidu.com',
            'sign_type' => 'md5',
        );
        $data['sign'] = $this->_MD5Sign($data, '9b389216c3c5vtlwf535510b33b9e6ea7');

        $this->render('alitestpay', array(
            'data' => $data
        ));
    }


    public function accessRules()
    {
        $isLoginExpression = 'false';
        $id_merchant = null;
        $id_wxplatform = Wxplatform::DEFAULT_ID;
        $mno = app()->request->getParam('mno', null);
        $mnoDe = Merchant::decodeMno($mno);
        $merchant = Merchant::model()->findByPk($mnoDe);
        if($merchant){
            $id_wxplatform = $merchant->id_wxplatform;
        }

        if(app()->user->getIsWxLogin($id_wxplatform)){
            $isLoginExpression = 'true';
            $isLoginRedirect = null;
        }else{
            $isLoginExpression = 'false';
            $isLoginRedirect = url('/wxauth/wxlogin', array(
                'callback_landing' => app()->request->getUrl(),
                'id_wxplatform' => $id_wxplatform
            ));
        }

        //跳过微信登录
        if($isLoginExpression=='false'){
            $this->user=Wxuser::model()->findByPk(932);
            $isLoginExpression = 'true';
        }

        return array(
            array(
                'allow',
                'actions' => array('active'),
                'expression' => $isLoginExpression,
                'redirect' => $isLoginRedirect
            ),

            array(
                'allow',
                'users' => array('*'),
            )
        );
    }
}
